package uz.edo.app.client;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;

import uz.edo.app.client.core.MySession;
import uz.edo.db.bean.User;
import uz.edo.db.manager.UserManager;

public class Login extends Interface{
    private User login;
    @SuppressWarnings("unchecked")
	private Form form;

    @SuppressWarnings("unchecked")
	public Login() {
    	super();
        login = new User();
        CompoundPropertyModel userModel = new CompoundPropertyModel(login);
        form = new Form("loginForm",userModel) {
        private static final long serialVersionUID = -6105445327728694947L;

			@Override
            protected void onSubmit() {
                try {
                    MySession session = (MySession)getSession();
                    User user = UserManager.getInstance().getUser(login.getUserName(), login.getPassword());
//                    if(user != null && "true".equals(user.getActive())) {
                    if (user != null && "Y".equals(user.getActive())) {
                        session.setUser(user);
                        setResponsePage(Home.class);
                    } else {
                        error(new ResourceModel("auth_failed").toString());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        form.add(new Link("uz")
        {
			private static final long serialVersionUID = 1L;
			@Override
        	public void onClick()
        	{
        		getSession().setLocale(LOCALE_UZ);
        		setResponsePage(Login.class);
        	}
        });
        form.add(new Link("ru")
        {
			private static final long serialVersionUID = 1L;
			@Override
        	public void onClick()
        	{
        		getSession().setLocale(LOCALE_RU);
        		setResponsePage(Login.class);
        	}
        });
        form.add(new RequiredTextField("userName").setLabel(new ResourceModel("auth_login")));
        form.add(new PasswordTextField("password").setLabel(new ResourceModel("auth_password")));
        form.add(new FeedbackPanel("feedback"));
        form.add(new Button("Submit",new ResourceModel("auth_enter")));

        add(form);
    }
}
